package com.controller;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.action.HistoryAction;
import com.bean.HistoryBean;
import com.exception.DBException;
import com.exception.HistoryException;
import com.util.HandleInfo;
import com.util.Regex;
import com.util.StateInfo;

public class HistoryServlet extends HttpServlet
{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		String key = request.getParameter("key");
		String value = request.getParameter("value");

		if (null == key || null == value)
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		if ("".equals(key) || "".equals(value))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}

		if (!"userid".equals(key) && !"barcode".equals(key))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}

		if ("barcode".equals(key) && !Regex.checkBarcode(value))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		if ("userid".equals(key) && !Regex.checkUserID(value))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		String date = request.getParameter("flag");
		if (null == date)//user does not choice the date for limitation
		{
			HistoryBean hb = new HistoryBean();
			hb.setKey(key);
			hb.setValue(value);
			try
			{
				new HistoryAction().getHistory(request, response, hb);
			}
			catch (DBException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
			catch (HistoryException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
		}
		if("date".equals(date))
		{
			String sDate = request.getParameter("sdate");
			String eDate = request.getParameter("edate");
			if(null == sDate || null == eDate)
			{
				HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			if(!Regex.checkDate(eDate) || !Regex.checkDate(sDate))
			{
				HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			HistoryBean hisBean = new HistoryBean();
			hisBean.setKey(key);
			hisBean.setValue(value);
			
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
			java.sql.Date startDate = null;
			java.sql.Date endDate = null;
			try
			{
				startDate = new Date(s.parse(sDate).getTime());
				endDate = new Date(s.parse(eDate).getTime());
			}
			catch (ParseException e)
			{
				HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			hisBean.setsDate(startDate);
			hisBean.seteDate(endDate);
			hisBean.setDate(true);
			try
			{
				new HistoryAction().getHistory(request, response, hisBean);
			}
			catch (DBException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
			catch (HistoryException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}

}
